<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: chen
  Date: 17-9-10
  Time: 上午10:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>this is list.jsp</title>
    <script src="/library/js/jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" href="/library/css/bootstrap.min.css">
    <script src="/library/js/bootstrap.min.js"></script>
    <style type="text/css">
        td{
            border: 1px solid black;
        }
    </style>
</head>
<body>
<%@include file="topUser.jsp"%>
    <c:choose>
        <c:when test="${empty books}">
            <h2>没有员工信息</h2>
        </c:when>
        <c:otherwise>
            <table class="table table-striped table-bordered table-hover  table-condensed" style="width: 60%;text-align: center">
                <caption align="top">图书列表(Book List)</caption>
                <thead>
                <tr>
                    <td>Id</td>
                    <td>Name</td>
                    <td>Number</td>
                    <td>Lease</td>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${books}" var="bookInfo">
                    <tr>
                        <!-- //定义class只为方便找到该兄弟节点 -->
                        <td class="book_id">
                                ${bookInfo.book_id}
                        </td>
                        <td>
                                ${bookInfo.book_name}
                        </td>
                        <td class="book_number">
                                ${bookInfo.book_number}
                        </td>
                        <td>
                            <a  class="lease">借阅</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:otherwise>
    </c:choose>

    <script type="text/javascript">
        $(function () {
            var a_lease = $("a.lease");
            var a_return = $("a.return");
            /**
             * 遍历全部的借阅按钮 如果数目小于等于0,则去除跳转功能.
             */
            a_lease.each(function () {
               if($(this).parents().siblings(".book_number").text() <= 0){
                   $(this).text("数目不足");
               }
            });
            /**
             * 定义点击事件,获取book_id和student_id,并用ajax和后台交换数据,获取结果.
             */
            a_lease.click(function () {
               /**
                * 此处当做是对父子兄弟节点的获取测试
                * @type {*|jQuery}
                */
//               var book_id = $(this).parents(".trtrtr").find("td.book_id").attr("hello"); //跳到顶级tr 在查找子节点中的td
               var book_id = $(this).parents().siblings(".book_id").text();    //查找兄弟节点,并获取book_id
               var book_number = $(this).parents().siblings(".book_number");   //获取book_number
//               var student_id = prompt("请输入学生id:");     //获取student_id 由弹窗输入
               var student_id  = ${sessionScope.get("loginUser").student.student_id};
               var URL = "/library/lease/"+$.trim(book_id)+"/leaseBook";    //构造URL
               if(student_id !== null){      //输入不为空时和后台交换信息
                   $.ajax({
                       url:URL,
                       type:"POST",
                       data:{
                           student_id:student_id
                       },
                       success:function (result) {
                           var leaseInf = result.data;      //LeaseExecution类型的操作结果
                           if(result.symbol) {   //处理结果为true
                               book_number.text(book_number.text() - 1);     //减少显示的书本数目
                           }else{   //处理结果为false
                               if (leaseInf.state === 0) {
                                   alert("库存不足");       //库存都显示在list页面中,可以省略
                               }
                               if (leaseInf.state === -1) {
                                   alert("书本不存在");       //书本信息存在不存在也在页面中
                               }
                               if(leaseInf.state === -2){
                                   alert("重复借阅");
                               }
                           }
                       }
                   });
               }
           })
        });


    </script>
</body>
</html>